package com.east.service.project.impl;


import java.util.Date;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.east.bean.Project;
import com.east.dao.ProjectMapper;
import com.east.service.project.ProjectService;
import com.east.utils.DateUtils;
import com.east.utils.Pagenation;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
@Service("projectService")
public class ProjectServiceImpl implements ProjectService{
			@Autowired
			ProjectMapper projectMapper;

			@Override
			public Page<Project> findByPage(Pagenation pagenation, String key,String startTime ,String endTime) {
				Page<Project> startPage = PageHelper.startPage(pagenation.getPageNum(), pagenation.getPageSize());
				if(StringUtils.isNotBlank(startTime)) {
					startTime="WXKJXM"+startTime.replace("-", "");
				}
				if(StringUtils.isNotBlank(endTime)) {
					endTime="WXKJXM"+endTime.replace("-", "");
				}else {
					String daysOfDate = DateUtils.getDaysOfDate(new Date());
					endTime="WXKJXM"+daysOfDate.replace("-", "");
				}
				projectMapper.findByAll(key, startTime, endTime);
				return startPage;
			}

			@Override
			public boolean saveProject(Project project) {
				Project pr = new Project();
				pr.setpUuid(UUID.randomUUID().toString());
				pr.setpDelFlag(1);
				pr.setpSerialNum(project.getpSerialNum());
				pr.setpName(project.getpName());//项目名称
				pr.setpForecost(project.getpForecost());//项目概算
				pr.setDepartmentId(project.getDepartmentId());//部门id
				pr.setpResponsible(project.getpResponsible());//项目负责人
				pr.setpDescription(project.getpDescription());//项目描述
				pr.setpRemark(project.getpRemark());//项目描述
				if(StringUtils.isNotBlank(project.getStartTime())) {
					pr.setpStartTime(DateUtils.getDate(project.getStartTime()));//开工时间
				}
				pr.setpEndTime(project.getpEndTime());//竣工时间
				pr.setpBreakUp(project.getpBreakUp());//是否结束
				pr.setpType(project.getpType());//项目类型
				pr.setpStatus(project.getpStatus());//项目状态
				pr.setpFinalAmount(project.getpFinalAmount());//决算金额      计算
				pr.setpResponsibleContact(project.getpResponsibleContact());//负责人联系方式
				pr.setpSaler(project.getpSaler());//销售负责人
				pr.setAddress(project.getAddress());
				int insert = projectMapper.insert(pr);
				if(insert<1) {
					return false;
				}else {
					return true;
				}
			}

			@Override
			public boolean updateProject(Project project) {
				Project pr = projectMapper.selectByPrimaryKey(project.getpId());
				pr.setpSerialNum(project.getpSerialNum());
				pr.setpName(project.getpName());//项目名称
				pr.setpForecost(project.getpForecost());//项目概算
				pr.setDepartmentId(project.getDepartmentId());//部门id
				pr.setpResponsible(project.getpResponsible());//项目负责人
				pr.setpDescription(project.getpDescription());//项目描述
				pr.setpRemark(project.getpRemark());//项目描述
				if(StringUtils.isNotBlank(project.getStartTime())) {
					pr.setpStartTime(DateUtils.getDate(project.getStartTime()));//开工时间
				}
				pr.setpEndTime(project.getpEndTime());//竣工时间
				pr.setpBreakUp(project.getpBreakUp());//是否结束
				pr.setpType(project.getpType());//项目类型
				pr.setpStatus(project.getpStatus());//项目状态
				pr.setpFinalAmount(project.getpFinalAmount());//决算金额      计算
				pr.setpResponsibleContact(project.getpResponsibleContact());//负责人联系方式
				pr.setpSaler(project.getpSaler());//销售负责人
				pr.setAddress(project.getAddress());
				int insert = projectMapper.updateByPrimaryKey(pr);
				if(insert<1) {
					return false;
				}else {
					return true;
				}
			}

			@Override
			public boolean delProject(Project project) {
				Project selectByPrimaryKey = projectMapper.selectByPrimaryKey(project.getpId());
				selectByPrimaryKey.setpDelFlag(0);
				int updateByPrimaryKey = projectMapper.updateByPrimaryKey(selectByPrimaryKey);
				if(updateByPrimaryKey<1) {
					return false;
				}else {
					return true;
				}
				
			}

			@Override
			public Project findByProjectId(Project project) {
				return projectMapper.selectByPrimaryKey(project.getpId());
			}
			
}
